package com.codechuan.hallo_internal.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.codechuan.hallo_internal.common.Constant;
import com.codechuan.hallo_internal.floatwindow.log.LogManager;
import com.codechuan.hallo_internal.utils.HalloProjectHttpUtil;

public class CheckJDThread implements Runnable{
    @Override
    public void run() {
        // 判断是否接单成功
        while (Constant.isStart){
            JSONObject jsonObject = HalloProjectHttpUtil.getWeChatMiniMyOrderOrMyScheduleOrder();
            if (jsonObject != null){
                if (jsonObject.getString("code").equals("0")) {
                    // 获取行程数组
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (Object datum : data) {
                        JSONObject dataList = JSONObject.parseObject(datum.toString());
                        JSONArray paxOrderList = dataList.getJSONArray("paxOrderList");
                        for (Object o : paxOrderList) {
                            JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
                            String string = jsonObject1.getString("passengerJourneyGuid");
                            if (Constant.orderList.toString().isEmpty()) {
                                LogManager.get().addLog("接单成功:" + jsonObject1);
                                LogManager.get().stopLog();
                                if (!Constant.mediaPlayerPK.isPlaying()) {
                                    Constant.mediaPlayerPK.start();
                                }
                                // 判断是否连续中单
                                if (!Constant.LX_QD){
                                    Constant.isStart = false;
                                }
                            } else {
                                if (!Constant.orderList.toString().contains(string)) {
                                    LogManager.get().addLog("接单成功:" + jsonObject1);
                                    LogManager.get().stopLog();
                                    if (!Constant.mediaPlayerPK.isPlaying()) {
                                        Constant.mediaPlayerPK.start();
                                    }
                                    // 判断是否连续中单
                                    if (!Constant.LX_QD){
                                        Constant.isStart = false;
                                    }
                                }
                            }
                            // 重置订单列表初始化信息
                            if (Constant.LX_QD){
                                new Thread(() -> {
                                    JSONObject jsonObject2 = HalloProjectHttpUtil.getWeChatMiniMyOrderOrMyScheduleOrder();
                                    if (jsonObject2.getString("code").equals("0")) {
                                        // 获取行程数组
                                        JSONArray data1 = jsonObject2.getJSONArray("data");
                                        for (Object datum1 : data1) {
                                            JSONObject dataList1 = JSONObject.parseObject(datum1.toString());
                                            JSONArray paxOrderList1 = dataList1.getJSONArray("paxOrderList");
                                            for (Object o1 : paxOrderList1) {
                                                JSONObject jsonObject11 = JSONObject.parseObject(o1.toString());
                                                String string1 = jsonObject11.getString("passengerJourneyGuid");
                                                Constant.orderList.append(string1).append(",");
                                            }
                                        }
                                    }
                                }).start();
                            }

                        }
                    }
                }
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}